package firstTry;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class Rocket extends Sprite {

	Rectangle2D.Double target;
	boolean locked = false;
	
	final int SPEED = 100;

	public Rocket(BufferedImage[] i, double x, double y, long delay, GamePanel p) {
		super(i, x, y, delay, p);

		
		if (getY() < parent.getHeight() / 2) {
			setVerticalSpeed(SPEED);
		} else {
			setVerticalSpeed(-SPEED);
		}

	}
	

	

	public void doLogic(long delta) {
		super.doLogic(delta);
		
		if(getHorizontalSpeed()>0 && getX()>parent.getWidth())
			remove = true;
		if(getHorizontalSpeed()<0 && getX()+getWidth()<0)
			remove = true;

		if (getHorizontalSpeed() > 0) {
			target = new Rectangle2D.Double(getX() + getWidth(), getY(), parent
					.getWidth() - getX(), getHeight());
		} else {
			target = new Rectangle2D.Double(0, getY(), getX(), getHeight());
		}

		if (!locked && parent.copter.intersects(target)) {
			setVerticalSpeed(0);
			locked = true;
		}
		
		if(locked){
			if(getY()<parent.copter.getY()){
				setVerticalSpeed(SPEED-20);
			}
			if(getY()>parent.copter.getY()+parent.copter.getHeight()){
				setVerticalSpeed(-SPEED+20);
			}
		}
	}

	public void setHorizontalSpeed(double d) {
		super.setHorizontalSpeed(d);
		
		if(getHorizontalSpeed()>0)
			setLoop(4, 7);
		else
			setLoop(0, 3);

	}




	
	public boolean collidedwith(Sprite s) {
		
		if(remove)
			return false;
		
		
		if(this.intersects(s)){
			
			if (s instanceof Heli || s instanceof Rocket){
				if(super.checkOpaqueColorCollisions(s)){
				remove = true;
				s.remove = true;
				parent.createExplosion((int)getX(), (int)getY());
				parent.createExplosion((int)s.getX(), (int)s.getY());
				}
			}
		}

		return false;
	}

}
